﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using System.Data.SqlClient;
using Lamex.Base;
using Lamex.Library.CommonClass;
using Lamex.Interface;
using Lamex.Common;
using Lamex.mCapacity.UI;
using Lamex.mSchedule.UI;
using Lamex.mCapacity;
using Lamex.mSchedule;
using Lamex.mEfficiency.UI;

namespace Lamex.mMES.UI
{
    public partial class frmMES_Operation_Management : frmBaseChild
    {
        private static string strConn = SqlConfiguration.CurrentConnectionString;

        public frmMES_Operation_Management()
        {
            InitializeComponent();
        }

        private void frmMES_Operation_Management_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Add("运作管理");
            addTreeViewRootNode(treeView1);//添加根节点
            addTreeViewSubNode(treeView1);//添加子节点
            addTreeViewSubSubNode(treeView1);//添加子节点

            treeView1.ExpandAll();
        }

        private static DataTable ExcuteQuery(string sql)
        {
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            adp.Fill(dt);
            return dt;
        }

        #region 树形菜单加载

        //样例：https://www.cnblogs.com/net064/p/5534697.html
        //获取所有数据库，并加载treeView1
        private void addTreeViewRootNode(TreeView tr1)
        {
            //为了按数据表设定的顺序加载，所以SQL语句复杂化
            //            string sql = @"   SELECT DISTINCT OPERATION_ITEM
            //                        FROM OP_Operation_Function";
            string sql = @"						SELECT DISTINCT SORTING_INDEX,a.OPERATION_ITEM
						FROM
						(SELECT DISTINCT OPERATION_ITEM
                        FROM OP_Operation_Function) AS a
						LEFT JOIN 
						(SELECT OPERATION_ITEM,MIN(SORTING_INDEX) AS SORTING_INDEX
                        FROM OP_Operation_Function
						GROUP BY OPERATION_ITEM) AS b
						ON a.OPERATION_ITEM=b.OPERATION_ITEM
						ORDER BY SORTING_INDEX";

            DataTable dt = ExcuteQuery(sql);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    tr1.Nodes[0].Nodes.Add(dr["OPERATION_ITEM"].ToString());
                }
            }
        }

        private void addTreeViewSubNode(TreeView tr1)
        {
            //为了按数据表设定的顺序加载，所以SQL语句复杂化
            //            string sql = @"   SELECT DISTINCT OPERATION_ITEM,FUNCTION_ITEM
            //                        FROM OP_Operation_Function";
            string sql = @"						SELECT DISTINCT SORTING_INDEX,a.OPERATION_ITEM,a.FUNCTION_ITEM
						FROM
						(SELECT DISTINCT OPERATION_ITEM,FUNCTION_ITEM
                        FROM OP_Operation_Function) AS a
						LEFT JOIN 
						(SELECT FUNCTION_ITEM,MIN(SORTING_INDEX) AS SORTING_INDEX
                        FROM OP_Operation_Function
						GROUP BY FUNCTION_ITEM) AS b
						ON a.FUNCTION_ITEM=b.FUNCTION_ITEM
						ORDER BY OPERATION_ITEM,SORTING_INDEX";

            DataTable dt = ExcuteQuery(sql);

            if (dt.Rows.Count > 0)
            {
                foreach (TreeNode node in tr1.Nodes)
                {
                    getNodeRecurve(node, dt, "OPERATION_ITEM", "FUNCTION_ITEM");
                }
            }
        }

        private void addTreeViewSubSubNode(TreeView tr1)
        {
            string sql = @"   SELECT DISTINCT FUNCTION_ITEM,SUB_FUNCTION
                        FROM OP_Operation_Function";
            DataTable dt = ExcuteQuery(sql);

            if (dt.Rows.Count > 0)
            {
                foreach (TreeNode node in tr1.Nodes)
                {
                    getNodeRecurve(node, dt, "FUNCTION_ITEM", "SUB_FUNCTION");
                }
            }
        }

        private void getNodeRecurve(TreeNode tn, DataTable dt,string str1,string str2)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (tn.Text == dr[str1].ToString())
                {
                    tn.Nodes.Add(dr[str2].ToString());
                }
            }

            foreach (TreeNode tnSub in tn.Nodes)
            {
                getNodeRecurve(tnSub, dt, str1, str2);
            }
        }

        #endregion

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //MessageBox.Show(e.Node.Level.ToString());
            //return;

            //TO DO
            //（偶尔）连续双击第二级时，会指向上一节点的第三级，导致打开上一节点窗体
            string strMoudleName = "";
            if (e.Node.Level == 3)
            {
                strMoudleName = e.Node.Parent.Text;
            }
            else
            {
                return;
            }

            switch (strMoudleName)
            {
                default:
                    break;
                case "KPI":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Operation_KPI(), sender as ToolStripMenuItem);
                    break;
                case "数字日历":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Operation_Digital_Calendar(), sender as ToolStripMenuItem);
                    break;
                case "实时概览":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Operation_Workshop_Overview(), sender as ToolStripMenuItem);
                    break;
                
                case "产能报表":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("View_OP_Capacity_Optimized_Result", 0, false, true), sender as ToolStripMenuItem);
                    break;
                case "欠货分析":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmCAP_Owed_Analysis_Summarize(), sender as ToolStripMenuItem);
                    break;
                case "生产计划":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmCAP_Owed_Production_Plan(), sender as ToolStripMenuItem);
                    break;
                case "排产预估":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmCAP_Plan_Evaluation(), sender as ToolStripMenuItem);
                    break;

                case "位置查询":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmSCH_Position_Search_Table(), sender as ToolStripMenuItem);
                    break;
                case "欠货清单":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("View_OP_Follow_Owed_QTY", 0, false, true), sender as ToolStripMenuItem);
                    break;
                case "欠货概览":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmSCH_Owed_Analyze_Overall(), sender as ToolStripMenuItem);
                    break;
                case "分厂欠货":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmSCH_Owed_Analyze_SubFactory(), sender as ToolStripMenuItem);
                    break;

                case "入仓分析":
                    //MessageBox.Show("待开发...");
                    //break;
                case "工序产量":
                    //MessageBox.Show("待开发...");
                    //break;
                case "产量分析":
                    //MessageBox.Show("待开发...");
                    //break;
                case "工时统计":
                    //MessageBox.Show("待开发...");
                    //break;
                case "绩效核算":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmEFF_Efficiency_Analyse(), sender as ToolStripMenuItem);
                    break;
                case "入仓明细":
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("View_OP_Inside_Detail", 0, false, true), sender as ToolStripMenuItem);
                    break;
            }
        }

        #region 树形菜单操作

        private void tsmExpand_All_Click(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
        }

        private void tsmExpand_Level1_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();
            foreach (TreeNode n in treeView1.Nodes)
            {
                if (n.Level == 0)
                {
                    treeView1.SelectedNode = n;
                    n.Expand();
                    return;
                }
            }
        }

        private void tsmExpand_Level2_Click(object sender, EventArgs e)
        {
            //2级以下收缩
            foreach (TreeNode tn in treeView1.Nodes)
            {
                CollapseNodeToLevel2(tn);
            }

            //展开1级
            foreach (TreeNode tn in treeView1.Nodes)
            {
                ExpandLevel1(tn);
            }
        }

        private void CollapseNodeToLevel2(TreeNode tn)
        {
            foreach (TreeNode tnSub in tn.Nodes)
            {
                if (tnSub.Level == 1)
                {
                    treeView1.SelectedNode = tnSub;
                    tnSub.Collapse();
                }
                else
                {
                    CollapseNodeToLevel2(tnSub);
                }
            }
        }

        private void ExpandLevel1(TreeNode tn)
        {
            foreach (TreeNode tnSub in tn.Nodes)
            {
                if (tnSub.Level == 1)
                {
                    treeView1.SelectedNode = tnSub;
                    tnSub.Expand();
                }
                else
                {
                    ExpandLevel1(tnSub);
                }
            }
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("OP_Operation_Function", 0, true, true), sender as ToolStripMenuItem);
        }
    }
}